﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace ZDT.Http
{
    public class TcpClientor:TcpClient
    {
        public TcpClientor()
            : base()
        {
        }
        public TcpClientor(string hostName,int port)
            : base(hostName, port)
        {

        }
        public void Connect(string hostName, int port)
        {
            Socket socket = this.Client;
            SocketAsyncEventArgs e = new SocketAsyncEventArgs();
            e.Completed += new EventHandler<SocketAsyncEventArgs>(ConnectEventArg_Completed);  
            socket.ConnectAsync(e);
        }
        void ConnectEventArg_Completed(object sender, SocketAsyncEventArgs connectEventArgs)  
        {  
            try  
            {
                SocketAsyncEventArgs e = new SocketAsyncEventArgs();
                e.Completed += new EventHandler<SocketAsyncEventArgs>(ReceiveEventArg_Completed);
                connectEventArgs.AcceptSocket.ReceiveAsync(e); //投递接收请求   
            }  
            catch (Exception ex)  
            {  
            }              
        }
        void ReceiveEventArg_Completed(object sender, SocketAsyncEventArgs receiveEventArgs)
        {
            try
            {
                SocketAsyncEventArgs e = receiveEventArgs;

                receiveEventArgs.AcceptSocket.ReceiveAsync(e); //投递接收请求   
            }
            catch (Exception ex)
            {
            }
        }  
    }
}
